package pl.edu.uj.tcs.blef.protocol;

public class RejectRequestFactory implements RequestFactory {

	@Override
	public Request build(Object[] parameters, RequestHandler handler) throws BuildFailedException {
		if(parameters.length != 1) throw new BuildFailedException("One parameter excepted");
		try{
			String playerNick = (String)parameters[0];
			return new RejectRequest("/reject "+playerNick, handler);
		} catch(ClassCastException e) { throw new BuildFailedException(e.getMessage()); }
	}

	@Override
	public Request parse(String message, RequestHandler handler) throws ParseFailedException, BuildFailedException {
		if(message.split(" ").length != 2) throw new ParseFailedException("One argument expected.");
		if("/reject".equals(message.split(" ")[0])){
			Object tab[] = new Object[1];
			tab[0] = message.split(" ")[1];
			return build(tab, handler);
		}
		else throw new ParseFailedException("Unknown request.");
	}

}
